#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Muto Robot Controller API Models

This module contains all Pydantic models and enums used by the Muto Robot Controller API.
"""

from typing import List, Dict, Any, Optional
from pydantic import BaseModel
from enum import Enum



class PlanStatus(str, Enum):
    """规划状态枚举"""
    SUCCESS = "success"
    FAILED = "failed"
    PARTIAL = "partial"


class CommandPlan(BaseModel):
    """单个指令计划"""
    id: str
    command: str
    description: str
    critical: bool = False
    dependencies: List[str] = []
    parallel_group: Optional[str] = None  # 并行执行组标识
    sync_with: List[str] = []  # 需要同步的指令ID列表
    fetch_next_commands: bool = False  # 是否需要获取下一步指令序列


class ExecutePlanRequest(BaseModel):
    """执行指令序列请求"""
    status: PlanStatus
    plan: List[CommandPlan]
    need_broadcast: bool = False  # 是否需要在任务完成后进行播报
    suggestions: Optional[str] = None
    limitations: Optional[str] = None


class ExecutedCommand(BaseModel):
    """执行的命令信息"""
    description: str
    data: Optional[Dict[str, Any]] = None  # 命令返回的数据


class ExecutePlanResponse(BaseModel):
    """执行指令序列响应"""
    success: bool
    executed_commands: List[ExecutedCommand]  # 包含完整执行信息
    failed_commands: List[Dict[str, Any]] = []  # 保留完整错误信息
    # 下面这些字段用于提供更详细的失败原因与上下文，便于排查问题
    message: Optional[str] = None  # 执行摘要或错误摘要
    execution_time: Optional[float] = None  # 执行耗时（秒）
    failed_command_id: Optional[str] = None  # 第一个失败的命令ID（便于快速定位）
    error_details: Optional[List[Dict[str, Any]]] = None  # 每个失败命令的详细错误信息


class VoiceType(str, Enum):
    """语音类型枚举"""
    SYSTEM_SOUND = "system_sound"
    COMMON_RESPONSE = "common_response"
    CUSTOM_TEXT = "custom_text"


class PlayVoiceRequest(BaseModel):
    """播放语音请求"""
    voice_type: VoiceType
    content: str
    volume: Optional[float] = None


class PlayVoiceResponse(BaseModel):
    """播放语音响应模型"""
    success: bool
    message: str
    voice_type: str
    content: str


class JSONPlanRequest(BaseModel):
    """JSON格式的指令序列请求"""
    status: str
    plan: List[Dict[str, Any]]
    need_broadcast: bool = False  # 是否需要在任务完成后进行播报


class JSONPlanResponse(BaseModel):
    """JSON格式计划响应模型"""
    success: bool
    message: str
    executed_commands: List[Dict[str, Any]]
    execution_time: Optional[float] = None
    failed_command_id: Optional[str] = None


class WrappedPlanRequest(BaseModel):
    """包装格式的计划请求模型 - 外层包含result字段"""
    result: ExecutePlanRequest


class VoiceModuleStatusResponse(BaseModel):
    """Voice模块状态响应模型"""
    success: bool
    message: str
    module_status: Optional[Dict[str, bool]] = None
    queue_status: Optional[Dict[str, Any]] = None


class PlaySystemSoundRequest(BaseModel):
    """播放系统音乐请求"""
    sound_name: str
    volume: Optional[float] = None


class PlaySystemSoundResponse(BaseModel):
    """播放系统音乐响应模型"""
    success: bool
    message: str
    sound_name: str
    language: str


class TTSRequest(BaseModel):
    """文本转语音请求模型"""
    text: str
    output_filename: Optional[str] = None  # 可选的输出文件名，不指定则自动生成
    volume: Optional[float] = None  # 播放音量 0.0-1.0
    max_retries: Optional[int] = 3  # 最大重试次数
    save_file: Optional[bool] = True  # 是否保存文件，默认保存


class TTSResponse(BaseModel):
    """TTS响应模型"""
    success: bool
    message: str
    text: str  # 转换的原始文本
    output_file: Optional[str] = None  # 生成的音频文件路径
    audio_duration: Optional[float] = None  # 音频时长（秒）
    file_size: Optional[int] = None  # 文件大小（字节）
    synthesis_time: Optional[float] = None  # 合成耗时（秒）


# ==================== 记忆功能相关模型 Memory Function Models ====================

class WriteLongTermMemoryRequest(BaseModel):
    """写入长期记忆请求"""
    content: str
    timestamp: Optional[str] = None  # 可选时间戳，不提供则自动生成


class WriteLongTermMemoryResponse(BaseModel):
    """写入长期记忆响应"""
    success: bool
    message: str
    timestamp: str


class ReadLongTermMemoryRequest(BaseModel):
    """读取长期记忆请求"""
    timestamp: Optional[str] = None  # 指定时间戳读取特定记忆
    start_time: Optional[str] = None  # 时间范围查询开始时间
    end_time: Optional[str] = None    # 时间范围查询结束时间
    limit: Optional[int] = None


class ReadLongTermMemoryResponse(BaseModel):
    """读取长期记忆响应"""
    success: bool
    message: str
    memories: Optional[List[Dict[str, Any]]] = None


class WriteShortTermMemoryRequest(BaseModel):
    """写入短期记忆请求"""
    content: str
    timestamp: Optional[str] = None  # 可选时间戳，不提供则自动生成
    ttl_seconds: Optional[int] = None


class WriteShortTermMemoryResponse(BaseModel):
    """写入短期记忆响应"""
    success: bool
    message: str
    timestamp: str
    ttl_seconds: Optional[int] = None


class ReadShortTermMemoryRequest(BaseModel):
    """读取短期记忆请求"""
    timestamp: Optional[str] = None  # 指定时间戳读取特定记忆
    start_time: Optional[str] = None  # 时间范围查询开始时间
    end_time: Optional[str] = None    # 时间范围查询结束时间


class ReadShortTermMemoryResponse(BaseModel):
    """读取短期记忆响应"""
    success: bool
    message: str
    memories: Optional[List[Dict[str, Any]]] = None


class ClearShortTermMemoryRequest(BaseModel):
    """清除短期记忆请求"""
    timestamp: Optional[str] = None  # 指定时间戳清除特定记忆
    start_time: Optional[str] = None  # 时间范围清除开始时间
    end_time: Optional[str] = None    # 时间范围清除结束时间


class ClearShortTermMemoryResponse(BaseModel):
    """清除短期记忆响应"""
    success: bool
    message: str
    cleared_count: Optional[int] = None


class MemoryStatusResponse(BaseModel):
    """记忆系统状态响应模型"""
    success: bool
    message: str
    status: Optional[Dict[str, Any]] = None


# ==================== Warm Up Squat Action Models ====================

class WarmUpSquatRequest(BaseModel):
    """热身蹲起动作请求模型"""
    action: str  # "start" or "stop"


class WarmUpSquatResponse(BaseModel):
    """热身蹲起动作响应模型"""
    success: bool
    message: str
    action: str  # "start" or "stop"
    data: Optional[Dict[str, Any]] = None


# ==================== Raise Hand Action Models ====================

class RaiseHandRequest(BaseModel):
    """举手动作请求模型"""
    side: str  # "left" 或 "right"
    hold_time: Optional[float] = None
    amplitude: Optional[int] = None
    speed_ms: Optional[int] = None


class RaiseHandResponse(BaseModel):
    """举手动作响应模型"""
    success: bool
    message: str
    side: str
    data: Optional[Dict[str, Any]] = None


# ==================== Point Front Action Models ====================

class PointFrontRequest(BaseModel):
    """指向前方动作请求模型"""
    side: str  # "left" 或 "right"
    hold_time: Optional[float] = None
    amplitude: Optional[int] = None
    speed_ms: Optional[int] = None


class PointFrontResponse(BaseModel):
    """指向前方动作响应模型"""
    success: bool
    message: str
    side: str
    data: Optional[Dict[str, Any]] = None


# ==================== 全局取消/恢复控制模型 ====================

class CancelControlResponse(BaseModel):
    """全局取消控制响应模型"""
    success: bool
    message: str
    canceled_at: Optional[float] = None  # 取消触发时间戳（秒）


class ResumeControlResponse(BaseModel):
    """全局恢复控制响应模型"""
    success: bool
    message: str
